/*
 * @Author: xinyang3
 * @Date: 2021-11-16 18:04
 * @Descripttion: 将md字符流转换成html字符串
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-11-16 22:35:24
 */
import * as showdown from "showdown";

const RegImage = /.*\/(.*\.(?:jpg|JPG|png|PNG|gif|GIF){1})$/;
const showdownMd = showdown.default;
showdownMd.extension("myext", function () {
  return [{
    type: "output",
    filter: function (html, converter, options) {
      let container = document.createElement("div");
      container.innerHTML = html;
      let images = container.querySelectorAll("img");
      images &&
        images.forEach((item) => {
          let detailId = converter.getOption("customDetailId");
          let originSrc = item.src;
          let match = originSrc.match(RegImage);
          if (match && match.length > 1) {
            let name = match[1];
            let src = `${window.location.origin}/bgmg/blog/detail/${detailId}/${name}`;
            item.setAttribute("src", src);
          }
        });
      return container.innerHTML;
    },
  }, ];
});

const converter = new showdownMd.Converter({
  extensions: ["myext"]
});
converter.setFlavor("github");

export default converter;